/*
 * Copyright 2007-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.paoding.rose.web.impl.thread;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import net.paoding.rose.web.annotation.Param;

import org.apache.commons.lang.StringUtils;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * 
 * @author qieqie.wang@paoding.net
 * 
 */
public class ParameterNameDiscovererImpl implements ParameterNameDiscoverer {

	public Param[] getParamAnnotations(Method method) {
		Class<?>[] parameterTypes = method.getParameterTypes();
		Annotation[][] parameterAnnotations = method.getParameterAnnotations();
		Param[] paramAnnotations = new Param[parameterTypes.length];
		for (int i = 0; i < paramAnnotations.length; i++) {
			Annotation[] annotations = parameterAnnotations[i];
			for (Annotation annotation : annotations) {
				if (annotation instanceof Param) {
					paramAnnotations[i] = ((Param) annotation);
				}
			}
		}
		return paramAnnotations;
	}

	@Override
	public String[] getParameterNames(Method method) {
		Class<?>[] parameterTypes = method.getParameterTypes();
		Annotation[][] parameterAnnotations = method.getParameterAnnotations();
		String[] names = new String[parameterTypes.length];
		Map<String, Integer> counts = new HashMap<String, Integer>();
		for (int i = 0; i < names.length; i++) {
			Annotation[] annotations = parameterAnnotations[i];
			for (Annotation annotation : annotations) {
				if (annotation instanceof Param) {
					String name = ((Param) annotation).value();
					if (StringUtils.isNotBlank(name)) {
						names[i] = name;
						break;
					}
				}
			}
			if (names[i] != null) {
				continue;
			}
			if (ClassUtils.isPrimitiveOrWrapper(parameterTypes[i])
					|| parameterTypes[i] == String.class
					|| Collection.class.isAssignableFrom(parameterTypes[i])
					|| parameterTypes[i].isArray()) {
				continue;
			}
			String rawName = getParameterRawName(parameterTypes[i]);
			names[i] = rawName;
			Integer count = counts.get(rawName);
			if (count == null) {
				counts.put(rawName, 1);
			} else {
				counts.put(rawName, count + 1);
				if (count == 1) {
					for (int j = 0; j < i; j++) {
						if (names[j].equals(rawName)) {
							names[j] = rawName + "1";
							break;
						}
					}
				}
				if (names[i] == rawName) {
					names[i] = names[i] + (count + 1);
				}
			}
		}
		Set<String> uniques = new HashSet<String>();
		for (String name : names) {
			if (name == null) {
				continue;
			}
			if (uniques.contains(name)) {
				throw new IllegalArgumentException("action方法不能有相同名字的@Param参数"
						+ name);
			}
			uniques.add(name);
		}
		return names;
	}

	protected String getParameterRawName(Class<?> clz) {
		if (clz.isArray()) {
			return getParameterRawName(clz.getComponentType()) + 's';
		}
		if (clz == MultipartFile.class) {
			return "file";
		}
		return ClassUtils.getShortNameAsProperty(clz);
	}

	@SuppressWarnings("unchecked")
	@Override
	public String[] getParameterNames(Constructor ctor) {
		throw new UnsupportedOperationException();
	}
}
